package team.sun.sss.controller.hardwork;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.sun.sss.dao.hardwork.HwApplyDao;
import team.sun.sss.dao.hardwork.HwBatchDao;
import team.sun.sss.dao.hardwork.HwStatisticsDao;
import team.sun.sss.dao.hardwork.HwTimeDao;
import team.sun.sss.dao.student.FamilyDao1;
import team.sun.sss.dao.student.StudentDao;

import team.sun.sss.pojo.hardwork.*;
import team.sun.sss.pojo.student.Family;
import team.sun.sss.pojo.student.Student1;
import team.sun.sss.pojo.user.User;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Controller
public class HwApplyController {
    @Autowired
    HwApplyDao hwApplyDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    FamilyDao1 familyDao1;
    @Autowired
    HwTimeDao hwTimeDao;
    @Autowired
    HwStatisticsDao hwStatisticsDao;
    @Autowired
    HwBatchDao hwBatchDao;
    //1.显示学生自己的申请列表
    @RequestMapping("/hw_stu_apply")
    public String hwStuApply(Model model,HttpSession session){
        User user = (User) session.getAttribute("loginInfo");
        model.addAttribute("stuApplyList",hwApplyDao.getHwApplyList(user.getUserId()));
        return "hardWork/hw_stu_apply";

    }
    @GetMapping("/to_hw_sw_writeApply")
    public  String toWriteApply(Model model, HttpSession session){
        User user = (User) session.getAttribute("loginInfo");
        model.addAttribute("studentList",studentDao.selectStuById(user.getUserId()));
        model.addAttribute("familyList",familyDao1.getFamilyList(user.getUserId()));
        model.addAttribute("team",hwBatchDao.getTeamName());
        return "hardWork/hw_stu_applyWrite";
    }
    //1.2学生填写申请表
    @GetMapping("/hw_stu_writeApply")
    public String writeStuApply(Model model, HttpSession session,
                                String bankId,String province,String city,
                                 Float debt,String award,Float monthMoney,String orn,
                                String perform,String unit,String pastUnit,String canworkTime,
                                String batchTime,String familyLevelExplain){
        User user = (User) session.getAttribute("loginInfo");
        HwStatistics hwStatistics1=hwStatisticsDao.getRecruiters(batchTime,unit);
        HwStatistics hwStatistics2=hwStatisticsDao.getNowState(batchTime,unit);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now();
        String submitTime = today.format(fmt);
        String  hwaApplyState="未审核";
        String hwaWorkState="未入职";
        if(hwStatistics1!=null && hwStatistics2!=null) {
            if (hwStatistics1.getLaborRecruitersNum() == hwStatistics2.getHwStateNow()) {
                model.addAttribute("studentList",studentDao.selectStuById(user.getUserId()));
                model.addAttribute("familyList",familyDao1.getFamilyList(user.getUserId()));
                model.addAttribute("team",hwBatchDao.getTeamName());
                model.addAttribute("msg1", "该人数已满");

                return "hardWork/hw_stu_applyWrite";
            }
        }else{
            hwApplyDao.submitHwApply(user.getUserId(),debt,monthMoney,award,orn,
                    perform,unit,pastUnit,canworkTime,hwaApplyState,
                    hwaWorkState,submitTime,batchTime,familyLevelExplain);
            studentDao.applyUpdateStu(user.getUserId(),bankId,province,city);
        }
       model.addAttribute("msg1","success");
        model.addAttribute("stuApplyList",hwApplyDao.getHwApplyList(user.getUserId()));
        return "hardWork/hw_stu_apply";
    }

    //1.3查看某个详细的学生申请表
    @GetMapping("/hw_stu_applyDetail/{num}")
    public String stuApplyDetail(@PathVariable("num") Integer num, Model model,HttpSession session){
        //查出原来的数据
        User user = (User) session.getAttribute("loginInfo");
        List<Student1> selectStuById= studentDao.selectStuById(user.getUserId());
        model.addAttribute("studentList",selectStuById);
        //家长信息
        List<Family> familyList = familyDao1.getFamilyList(user.getUserId());
        model.addAttribute("familyList",familyList);
        //学生申请信息
        model.addAttribute("stuApplyDetail", hwApplyDao.getHwApplyDetail(num));
        return "hardWork/hw_stu_applyDetail";
    }
    //2.1学工显示所有学生申请列表
    @GetMapping("/hw_staff_stuApply")
    public String staffStuApply(Model model,@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 5;    //设置默认每页显示的数据数
        }
        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<HwApply> studentList = hwApplyDao.stuApplyList();
            model.addAttribute("getStuHwApplyList",studentList);
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            System.out.println("分页数据："+studentList);
            PageInfo<HwApply> pageInfo = new PageInfo<HwApply>(studentList,pageSize);
            //4.使用model/map/modelandview等带回前端
            model.addAttribute("pageInfo",pageInfo);
        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        model.addAttribute("teamList",hwBatchDao.getTeamName());
        return "hardWork/hw_staff_stuApply";
    }
    @RequestMapping("staff_stuApply_select")
    public String staff_stuApply_select(Model model,String batchTeam,String unit,String workState,String stuName,Integer stuId){
        model.addAttribute("teamList",hwBatchDao.getTeamName());
        System.out.println(stuName);
        if(batchTeam.equals(0)&&unit.equals(1)&&workState.equals(2)&&(stuName!=null)&&(stuId==null)){
            model.addAttribute("getStuHwApplyList",hwApplyDao.selectApplyByName(stuName));
        }else if(stuId!=null){
            model.addAttribute("getStuHwApplyList",hwApplyDao.selectApplyById(stuId));
        }else{
            model.addAttribute("getStuHwApplyList",hwApplyDao.selectApply(batchTeam,unit,workState));
        }

        return "hardWork/hw_staff_stuApply";
    }
    //2.2显示某个申请表的详情，不能只靠学号判断，因为可能学生提交了多张表
    @GetMapping("/hw_staff_stuApplyDetail/{num}/{stuId}")
    public String staffstuApplyDetail(@PathVariable("num") Integer num,@PathVariable("stuId") Integer stuId,
                                      Model model){
        List<Student1> selectStuById= studentDao.selectStuById(stuId);
        model.addAttribute("studentList",selectStuById);
        //家长信息
        List<Family> familyList = familyDao1.getFamilyList(stuId);
        model.addAttribute("familyList",familyList);
        //学生申请信息
        model.addAttribute("stuApplyDetail", hwApplyDao.getHwApplyDetail(num));
        return "hardWork/hw_staff_stuApplyDetail";
    }
    @GetMapping("/hw_staff_stuApplyUpdate/{num}/{stuId}")
    public String staffstuApplyUpdate(@PathVariable("num") Integer num,@PathVariable("stuId") Integer stuId,
                                      Model model){
        //学生信
        model.addAttribute("studentList",studentDao.selectStuById(stuId));
        //家长信息
        model.addAttribute("familyList",familyDao1.getFamilyList(stuId));
        //学生申请信息
        model.addAttribute("stuApplyDetail", hwApplyDao.getHwApplyDetail(num));

        return "hardWork/hw_staff_stuApplyUpdate";
    }

    @RequestMapping("/hw_staff_stuApplyUpdate")
    public String staffupdatestuApply(Model model,
                                      Integer hwaNum,String hwaApplyState, String hwaWorkState, String hwaEnterTime,
                                      Integer stuId,String batchTeam,String unit){
        hwApplyDao.updateHwApply( hwaNum, hwaApplyState, hwaWorkState, hwaEnterTime);
        //同时插入工时表
       if(hwaApplyState.equals("审核通过") && hwaWorkState.equals("在职")){

           hwTimeDao.insertTimeTable(stuId,unit,batchTeam);
           hwStatisticsDao.updateHwBatch(batchTeam,unit);
       }
        model.addAttribute("getStuHwApplyList",hwApplyDao.getStuHwApplyList());
        return "hardWork/hw_staff_stuApply";
    }


}
